﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Modules;
using WP.DB;
using WP.Controls;
using WP.Commons;
using System.Collections;

namespace WP.IM.Form
{
    public partial class frmItemPriceFormula : BaseQryRpt 
    {
        private enum DtlCol
        {
            SeqNo = 0,
            Type,
            CHNName,    //对应的中文名称
            Description, //描述
            FieldName,   //对应的字段名称
            TableName,   //对应的字段所在的表明
            RecID,
        }
        private bool mblnPressExit = true;
        private string mstrFormula = "";
        private double mdblPrice = 0;
        private string mstrItemRecID = "";
        private DBAccess mdbcon = new DBAccess ( false,false );
        public frmItemPriceFormula()
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            this.MinimumSize = this.Size;
            InitGridItem();
        }

        public bool ShowFormula(ref string strFormula, ref double dblPrice,string strItemRecID)
        {
           mstrItemRecID = strItemRecID ;
           txtFormula.Text = strFormula;
            this.ShowDialog();
            if (!mblnPressExit)
            {
                strFormula = txtFormula.Text;
                dblPrice = mdblPrice;
                return true;
            }
            mdbcon.CloseCon();
            return false;
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void InitGridItem()
        {
            this.dgvItem.Columns.Clear();
            this.dgvItem.Rows.Clear();

            DataGridViewTextBoxColumn colSeqNo = new DataGridViewTextBoxColumn();
            colSeqNo.HeaderText = "序号";
            colSeqNo.Visible = false;
            colSeqNo.Name = "SeqNo";
            colSeqNo.Width = 30;
            colSeqNo.ReadOnly = true;

            DataGridViewTextBoxColumn colType = new DataGridViewTextBoxColumn();
            colType.HeaderText = "类别";
            colType.Width = 50;
            colType.Name = "";
            colType.ReadOnly = false;
            colType.Visible = false;

            DataGridViewTextBoxColumn colWKName = new DataGridViewTextBoxColumn();
            colWKName.HeaderText = "名称";
            colWKName.Width = 150;
            colWKName.Name = "";
            colWKName.ReadOnly = false;

            DataGridViewTextBoxColumn colDescription = new DataGridViewTextBoxColumn();
            colDescription.HeaderText = "描述";
            colDescription.Width = 150;
            colDescription.Name = "";
            colDescription.ReadOnly = false;

            DataGridViewTextBoxColumn colFieldName = new DataGridViewTextBoxColumn();
            colFieldName.HeaderText = "字段";
            colFieldName.Width = 0;
            colFieldName.Name = "FieldName";
            colFieldName.ReadOnly = true;
            colFieldName.Visible = false;

            DataGridViewTextBoxColumn colTableName = new DataGridViewTextBoxColumn();
            colTableName.HeaderText = "表名";
            colTableName.Width = 80;
            colTableName.Name = "TableName";
            colTableName.ReadOnly = false;
            colTableName.Visible = false;

            DataGridViewTextBoxColumn colRecID = new DataGridViewTextBoxColumn();
            colRecID.HeaderText = "RecID";
            colRecID.Visible = false;
            colRecID.Name = "RecID";

            this.dgvItem.Columns.AddRange(new DataGridViewColumn[] { colSeqNo, colType, colWKName, colDescription, colFieldName, colTableName, colRecID });

            this.dgvItem.EditMode = DataGridViewEditMode.EditOnEnter;
            this.dgvItem.RowTemplate.Height = 20;
            this.dgvItem.RowHeadersWidth = 23;
            this.dgvItem.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.dgvItem.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.dgvItem.ColumnHeadersHeight = 30;
            this.dgvItem.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dgvItem.SelectionMode = DataGridViewSelectionMode.CellSelect;
            this.dgvItem.AllowUserToResizeRows = false;
            FillGrid();
        }

        private void FillGrid()
        {
            this.dgvItem.Rows.Clear();
            string strSQL = "";
            string strSQLTmp ="";
          
            DataFunctions.DelTemporyTable(  mdbcon , "#rmWKCalDict");
            strSQL = @" CREATE TABLE [#rmWKCalDict](
	                    [RecID] [int] IDENTITY(1,1) NOT NULL,
	                    [SeqNo] [int] NOT NULL  DEFAULT ((0)),
	                    [RecStamp] [timestamp] NULL,
	                    [CHNFieldName] [varchar](50) DEFAULT ('‘’'),
	                    [SYSFieldName] [varchar](50) DEFAULT (''),
	                    [SYSTableName] [varchar](50) NOT NULL DEFAULT (''),
	                    [Type] [varchar](50) NOT NULL  DEFAULT (''),
	                    [Description] [varchar](500)  NOT NULL  DEFAULT ('')
                    )";
            mdbcon.ExecuteSql(strSQL); 

            strSQL =@" INSERT INTO #rmWKCalDict( CHNFieldName,Description,SysFieldName,SysTableName)
                       VALUES ('{0}','{1}','{2}','{3}')";
            strSQLTmp = string.Format ( strSQL ,"常用单价","","TreatCost","Iminvmaster");
            mdbcon.ExecuteSql(strSQLTmp); 
            strSQLTmp = string.Format(strSQL, "长度", "", "prodLength", "Iminvmaster");
            mdbcon.ExecuteSql(strSQLTmp);
            strSQLTmp = string.Format(strSQL, "宽度", "", "prodWidth", "Iminvmaster");
            mdbcon.ExecuteSql(strSQLTmp);
            strSQLTmp = string.Format(strSQL, "高度", "", "prodHeight", "Iminvmaster");
            mdbcon.ExecuteSql(strSQLTmp);
            strSQLTmp = string.Format(strSQL, "克重", "", "GramWeight", "Iminvmaster");
            mdbcon.ExecuteSql(strSQLTmp);
            strSQLTmp = string.Format(strSQL ,"单张纸厚度","","PaperHeight","Iminvmaster");
            mdbcon.ExecuteSql(strSQLTmp);
            strSQL = "select * from #rmWKCalDict";
            DataTable  dtFormula = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtFormula.Rows)
            {
                this.dgvItem.Rows.Add(); ;
                this.dgvItem.Rows[this.dgvItem.Rows.Count - 1].Cells[(int)DtlCol.CHNName].Value = CvtFunc.ToString(drRow["CHNFieldName"]);
                this.dgvItem.Rows[this.dgvItem.Rows.Count - 1].Cells[(int)DtlCol.Description].Value = CvtFunc.ToString(drRow["Description"]);
                this.dgvItem.Rows[this.dgvItem.Rows.Count - 1].Cells[(int)DtlCol.FieldName].Value = CvtFunc.ToString(drRow["SysFieldName"]);
                this.dgvItem.Rows[this.dgvItem.Rows.Count - 1].Cells[(int)DtlCol.TableName].Value = CvtFunc.ToString(drRow["SysTableName"]);
                this.dgvItem.Rows[this.dgvItem.Rows.Count - 1].Cells[(int)DtlCol.Type].Value = CvtFunc.ToString(drRow["Type"]);
            }
            this.dgvItem.EndEdit();

        }


        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (VaildCheck())
            {
                mblnPressExit = false ;
                this.Close();
            }
        }

        private void dgvItem_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex < 0) return;
            if (this.dgvItem.CurrentRow == null) return;
            if (CvtFunc.ToString(this.dgvItem.Rows[e.RowIndex].Cells[(int)DtlCol.CHNName].Value) != "")
            {
                string strSelCHNName = CvtFunc.ToString(this.dgvItem.Rows[e.RowIndex].Cells[(int)DtlCol.CHNName].Value);
                string strBefore = txtFormula.Text.Substring(0, txtFormula.SelectionStart);
                string strAfter = txtFormula.Text.Substring(txtFormula.SelectionStart, txtFormula.Text.Length - txtFormula.SelectionStart - txtFormula.SelectionLength);
                txtFormula.Text = strBefore + strSelCHNName + strAfter;
            }
            txtFormula.SelectionStart = txtFormula.Text.Length;
        }

        private bool VaildCheck()
        {
            //用来检查表达式
            string strWKFormula = txtFormula.Text;
            if (strWKFormula == "")
            {
                if (MessageBox.Show("您确定关系式为空？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    return true;
                }
            }
            string strSQL = "select * from #rmWKCalDict";
            DataTable dtDict = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drDict in dtDict.Rows)
            {
                strWKFormula = strWKFormula.Replace(CvtFunc.ToString(drDict["CHNFieldName"]), CvtFunc.ToString(drDict["SYSTableName"]) + "." + CvtFunc.ToString(drDict["SYSFieldName"]));
            }

         
            strSQL = @" select top 1 {0}   from iminvmaster where recid='{1}'";
            strSQL = string.Format(strSQL, strWKFormula,mstrItemRecID );            
            try
            {
                DataRow drQty = mdbcon.GetDataFirstRow(strSQL);
                mdblPrice = CvtFunc.ToDouble(drQty[0]);
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
                MessageBox.Show("表达式错误，请检查后重新输入。", SysVar.WarnTitle);
                return false;
            }
            return true;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            this.txtFormula.Text = "";
        }
    }
}
